package com.link.speechdemo;

/**
 * Created by link on 2016/11/3.
 */

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.util.Log;

import com.link.speechdemo.bean.MusicJsonBean;
import com.link.speechdemo.bean.WeatherJsonBean;
import com.link.speechdemo.bean.WeatherJsonBean.*;
import com.google.gson.Gson;
import com.link.speechdemo.utils.PlayMusic;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 判断服务类型类
 */
public class JudgeServiceType {
    private static final String SERVICE_OPEN_QA = "openQA";
    private static final String SERVICE_WEATHER = "weather";
    private static final String SERVICE_MUSIC = "music";
    private static final String SERVICE_APP = "app";
    private static final String SERVICE_BAIKE = "baike";
    private static final String SERVICE_CHAT = "chat";
    //天气
    private List<DataBean.ResultBean> resultBeanList = new ArrayList<>();
    private DataBean.ResultBean resultBean;
    //音乐
    private List<MusicJsonBean.DataBean.ResultBean> MResultList = new ArrayList<>();
    private MusicJsonBean.DataBean.ResultBean MResultBean;


    /**
     * 判断语义理解服务类型
     *
     * @param type 类型
     * @param data 返回的数据
     */
    public void Judge(String type, String data) {
        switch (type) {
            case SERVICE_OPEN_QA:
                AnalysisOpenQA(data);
                break;
            case SERVICE_WEATHER:
                AnalysisWeather(data);
                break;
            case SERVICE_MUSIC:
                AnalysisMusic(data);
                break;
            case SERVICE_APP:
                AnalysisApp(data);
                break;
            case SERVICE_BAIKE:
                AnalysisBaiKe(data);
                break;
            case SERVICE_CHAT:
                AnalysisChat(data);
            default:
                Singleton.getSInstance().init("很抱歉，我还不能理解这句话，不过相信我会尽快成长起来的。");
                break;
        }
    }

    /**
     * 解析音乐数据
     *
     * @param data
     */
    private List<MusicJsonBean.DataBean.ResultBean> AnalysisMusic(String data) {
        JSONObject aaa = null;
        try {
            aaa = new JSONObject(data);
            Gson gson = new Gson();
            MusicJsonBean jsonBean = gson.fromJson(String.valueOf(aaa), MusicJsonBean.class);
            String artist = jsonBean.getSemantic().getSlots().getArtist();
            String song = jsonBean.getSemantic().getSlots().getSong();
            List<MusicJsonBean.DataBean.ResultBean> resultB = jsonBean.getData().getResult();
            for (int i = 0; i < resultB.size(); i++) {
                MResultBean = new MusicJsonBean.DataBean.ResultBean();
                MResultBean.setSinger(resultB.get(i).getSinger());
                MResultBean.setSourceName(resultB.get(i).getSourceName());
                MResultBean.setName(resultB.get(i).getName());
                MResultBean.setDownloadUrl(resultB.get(i).getDownloadUrl());
                MResultList.add(MResultBean);
            }
            PlayMusic playMusic = new PlayMusic();
            playMusic.startPlay(artist, song, resultB.get(0).getDownloadUrl());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return MResultList;
    }

    /**
     * 解析问候数据
     *
     * @param data
     */
    private void AnalysisOpenQA(String data) {
        JSONObject aaa = null;
        try {
            aaa = new JSONObject(data);
            JSONObject ddd = aaa.getJSONObject("answer");
            String text = ddd.getString("text");
            Log.e("text", text);
            Singleton.getSInstance().init(text);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析天气数据
     *
     * @param data
     */
    private List<DataBean.ResultBean> AnalysisWeather(String data) {
        String strService = null;
        Log.e("jsonResult", "执行了");
        try {
            JSONObject aaa = new JSONObject(data);
            Log.e("aaa", aaa.toString());
            strService = aaa.getString("service");
            Log.e("strService", strService.toString());
            Gson gson = new Gson();
            WeatherJsonBean JsonBean = gson.fromJson(String.valueOf(aaa), WeatherJsonBean.class);
            List<DataBean.ResultBean> resultB = JsonBean.getData().getResult();
            for (int i = 0; i < resultB.size(); i++) {
                resultBean = new DataBean.ResultBean();
                resultBean.setCity(resultB.get(i).getCity());
                resultBean.setWeather(resultB.get(i).getWeather());
                resultBean.setTempRange(resultB.get(i).getTempRange());
                resultBeanList.add(resultBean);
                Log.e("weather", resultBean.getCity());
            }
            String city = resultBeanList.get(1).getCity();
            String weather = resultBeanList.get(1).getWeather();
            String temp = resultBeanList.get(1).getTempRange();
            StringBuffer param = new StringBuffer();
            param.append(city + ",");
            param.append("今日," + weather + ",");
            param.append("温度," + temp);
            Singleton.getSInstance().init(param.toString());
            Log.e("resultBeanList", resultBeanList.get(1).toString());
        } catch (JSONException e) {
            Singleton.getSInstance().init("很抱歉，我还不能理解这句话，不过相信我会尽快成长起来的。");
            e.printStackTrace();
        }
        return resultBeanList;
    }

    /**
     * 打开应用
     *
     * @param data
     */
    private void AnalysisApp(String data) {
        JSONObject aaa = null;
        String mAppName = null;
        try {
            aaa = new JSONObject(data);
            JSONObject ddd = aaa.getJSONObject("semantic");
            JSONObject eee = ddd.getJSONObject("slots");
            mAppName = eee.getString("name");
            Log.e("text", mAppName);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (mAppName != null && mAppName.length() != 0) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.MAIN");
            intent.addCategory("android.intent.category.LAUNCHER");
            PackageManager pm = MainActivity.instance.getPackageManager();
            List<ResolveInfo> installAppList = pm.queryIntentActivities(intent, 0);
            for (ResolveInfo info : installAppList) {
                String name = info.loadLabel(pm).toString();
                if (name.contains(mAppName)) {
                    String pkgname = info.activityInfo.packageName;
                    if ("com.android.contacts".equalsIgnoreCase(pkgname)) {
                        Uri uri = Uri.parse("content://contacts/people");
                        Intent i = new Intent("android.intent.action.VIEW", uri);
                        MainActivity.instance.startActivity(i);
                    } else {
                        intent = pm.getLaunchIntentForPackage(pkgname);
                        intent.addCategory("android.intent.category.LAUNCHER");
                        MainActivity.instance.startActivity(intent);
                    }
                    Singleton.getSInstance().init("正在打开" + mAppName);
                    return;
                }
            }
            Singleton.getSInstance().init("没有找到你所说的应用哦");
        }
    }

    /**
     * 解析百科数据
     *
     * @param data
     */
    public void AnalysisBaiKe(String data) {
        JSONObject aaa = null;
        try {
            aaa = new JSONObject(data);
            JSONObject ddd = aaa.getJSONObject("answer");
            String text = ddd.getString("text");
            Log.e("text", text);
            Singleton.getSInstance().init(text);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析闲聊数据
     *
     * @param data
     */
    public void AnalysisChat(String data) {
        JSONObject aaa = null;
        try {
            aaa = new JSONObject(data);
            JSONObject ddd = aaa.getJSONObject("answer");
            String text = ddd.getString("text");
            Log.e("text", text);
            Singleton.getSInstance().init(text);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
